package weaver.interfaces.zhlx.duss.action.financemodule.srmpmp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SrmPMPUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 资金计划提报审批流程提交归档时调用接口推送项目中心管理平台（SRM）
 * @ModifyLog:
 **/
public class WeaZjjhtbsplcPMPAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaZjjhtbsplcPMPAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SRM项目管理系统接口返回:";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select id from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            JSONArray zjjhList = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt3 where mainid=? and ly=4", mainid);
            while (rs.next()) {
                JSONObject dataJson = new JSONObject();
                dataJson.put("planId", Util.null2String(rs.getString("jhid")));
                dataJson.put("ecAmountTax", Util.null2String(rs.getString("cwfhjeyb")));
                dataJson.put("ecAmountTaxLocal", Util.null2String(rs.getString("cwfhjebb")));
                dataJson.put("ecCheckUserId", Util.null2String(rs.getString("cwfhrgh")));
                dataJson.put("ecCheckUserName", Util.null2String(rs.getString("cwfhr")));
                zjjhList.add(dataJson);
            }
            if (!zjjhList.isEmpty()) {
                JSONObject paramsJson = new JSONObject();
                paramsJson.put("planData", zjjhList);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaZjjhtbsplcPMPAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaZjjhtbsplcPMPAction>>>>>paramsJson=" + paramsJson);
                JSONObject resultJson = SrmPMPUtil.fundPlan(paramsJson, dataMap);
                writeLog(">>>>>WeaZjjhtbsplcPMPAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject returnDataJson = JSONObject.parseObject(resultJson.getString("data"));
                    if (!"200".equals(returnDataJson.getString("code"))) {
                        message += Util.null2String(returnDataJson.getString("message"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaZjjhtbsplcPMPAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}